package com.example.demo.Controller;

import com.example.demo.bean.Student;
import com.example.demo.service.UserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/users")
    public List<Student> getAllUsers(){
        return userService.list();
    }

    @GetMapping("/user/{id}")
    public Student getUserById(@PathVariable("id") Integer id){
        User user = (User) userService.getById(id);
        return (Student) user;
    }

    @PostMapping("/user")
    public String addUser(@RequestBody User user){
        if (userService.save((Student) user)){
            return "插入用户成功！";
        }
        else return "插入用户失败";
    }

    @PutMapping("/user")
    public String updateUser(@RequestBody User user){
        if (userService.updateById((Student) user)){
            return "更新用户成功！";
        }
        else return "更新用户失败";
}
}


